# * author:LHY
#*date:2022-1-19
#二阶段单独测试

from gurobipy import *
#订单数据结构
class Data:
    timepiece = 0  # 实际代表时刻 从0时刻开始 相当于区间加1
    OrderNumber = 0 #假设订单0为depot，n+1为depot
    multipleNum = 1  # 对应转换关系 需要手动计算在txt文件中读取
    orderNum = []  # 订单编号
    customerNo = [] #对应的顾客编号
    orderTime = []  # 订单到来时间
    prepareTime = []  # 需要的拣货时间
    orderDemand = []  # 需求量
    serviceTime = []  # 服务时间
    dueTime = []    #最迟时间的软时间窗
    timeMatrix = [[]]  # 记录订单到达时间的区间 定义w(i_s)
    # timePara = [[]]
    timeNum = []  # 每一时间切片前的订单数量 定义S(t_s)
#客户数据结构
class Customer:
    allNumber = 0   #包含配送中心
    # Cor_X_depot = 0
    # Cor_Y_depot = 0
    allCus = []
    cor_X = []
    cor_Y = []
    distanceMatrix = [[]]
#车辆数据结构
class Vehicle:
    vehicleNum = 0
    vehicleCapacity = 0

def readData(data, path):
    f = open(path, 'r')
    lines = f.readlines()
    data.OrderNumber = len(lines) - 8
    count = 0
    #print(len(lines))
    for line in lines:
        count = count + 1
        if count == 4:
            line = line[:-1].strip()
            str = re.split(r" +", line)
            data.timepiece=int(str[0])
            data.multipleNum = int(str[1])

        if count >= 9 and count < 9 + data.OrderNumber:
            line = line[:-1]
            str = re.split(r" +",line)
            data.orderNum.append(int(str[0]))
            data.customerNo.append(int(str[1]))
            data.orderTime.append(float(str[2]))
            data.prepareTime.append(float(str[3]))
            data.orderDemand.append(float(str[4]))
            data.serviceTime.append(float(str[5]))
            data.dueTime.append(float(str[6]))
    #timepiece s时刻是否能够配送
    data.timeMatrix = [([0] * data.timepiece) for p in range(data.OrderNumber-2)]    #初始化，防止浅拷贝 timepiece列 OrderNumber行
    for i in range(1,data.OrderNumber-1):
        for j in range(0, data.timepiece):
            if (data.orderTime[i] + data.prepareTime[i])/data.multipleNum < j :
                data.timeMatrix[i-1][j] = 1


    data.timeNum = [0] * data.timepiece
    for i in range(1, data.timepiece):
        for k in range(0,data.OrderNumber-2):
            data.timeNum[i] += data.timeMatrix[k][i]

    return data
def readCustomer(customer,path):
    f = open(path,'r')
    lines = f.readlines()
    customer.allNumber = len(lines) - 8
    count = 0
    # print(len(lines))
    for line in lines:
        count = count + 1
        if count == 4:
            line = line[:-1].strip()
            str = re.split(r" +", line)
            customer.allNumber = int(str[0])
            # customer.Cor_X_depot = float(str[1])
            # customer.Cor_Y_depot = float(str[2])
        if count >= 9 and count < 9 + customer.allNumber:
            line = line[:-1]
            str = re.split(r" +", line)
            customer.allCus.append(int(str[0]))
            customer.cor_X.append(float(str[1]))
            customer.cor_Y.append(float(str[2]))
    speed = 10.0
    customer.distanceMatrix = [([0] * customer.allNumber) for p in range(customer.allNumber)]
    for i in range(customer.allNumber):
        for j in range(customer.allNumber):
            temp = (customer.cor_X[i]-customer.cor_X[j]) ** 2 + (customer.cor_Y[i]-customer.cor_Y[j]) ** 2
            customer.distanceMatrix[i][j] = math.sqrt(temp)
            temp = 0
    return customer
def readVehicle(vehicle,path):
    f = open(path,'r')
    lines = f.readlines()
    count = 0
    # print(len(lines))
    for line in lines:
        count = count + 1
        if count == 4:
            line = line[:-1].strip()
            str = re.split(r" +", line)
            vehicle.vehicleNum = int(str[0])
            vehicle.vehicleCapacity = int(str[1])


    return vehicle



data = Data()
path1 = 'data.txt'
readData(data,path1)

###customer需要改
customer = Customer()
path2 = 'readcustomer.txt'
readCustomer(customer,path2)

vehicle = Vehicle()
path3 = 'readvehicle.txt'
readVehicle(vehicle,path3)
z = [0] * data.OrderNumber
for i in range(data.OrderNumber):
        z[i] = 0
for i in range(1,21):
    z[i]= 1
BigM = 100000
print(customer.distanceMatrix[data.customerNo[1]][data.customerNo[2]])

# x = {}
# rsk = {}
# tti = {}
# ttwi = {}
#
#
#
# model = Model('test')
#
# #x_ijk
# for i in range(data.OrderNumber):
#     for k in range(vehicle.vehicleNum):
#         for j in range(data.OrderNumber):
#             if (i != j):
#                 name = 'x_' + str(i) + '_' + str(j) + '_' + str(k)
#                 x[i, j, k] = model.addVar(0
#                                           , 1
#                                           , vtype=GRB.BINARY
#                                           , name=name)
# #rsk
# for k in range(vehicle.vehicleNum):
#     name = 'r' + '_' + str(k)
#     rsk[k] = model.addVar(0
#                         , 1
#                         , vtype=GRB.BINARY
#                         , name=name)
# #tti
# for i in range(data.OrderNumber):
#     name = 'tti_' + str(i)
#     tti[i] = model.addVar(0
#                            , 1500
#                            , vtype=GRB.CONTINUOUS
#                            , name=name)
# #ttwi
# for i in range(data.OrderNumber):
#     name = 'ttwi_' + str(i)
#     ttwi[i] = model.addVar(0
#                                , 150
#                                , vtype=GRB.CONTINUOUS
#                                , name=name)
# model.update()
#
# obj = LinExpr(0)
# for i in range(data.OrderNumber):
#     for k in range(vehicle.vehicleNum):
#         for j in range(data.OrderNumber):
#             if(i!=j):
#                 obj.addTerms(customer.distanceMatrix[data.customerNo[i]][data.customerNo[j]],x[i,j,k])
#
# for k in range(vehicle.vehicleNum):
#     obj.addTerms(10,rsk[k])
# for i in range(1,data.OrderNumber-1):
#     obj.addTerms(1,ttwi[i])
# model.setObjective(obj,GRB.MINIMIZE)
#
# for i in range(1,data.OrderNumber-1):
#     lhs = LinExpr(0)
#     for k in range(vehicle.vehicleNum):
#         for j in range(1,data.OrderNumber):
#             if i!=j :
#                 lhs.addTerms(1,x[i,j,k])
#     model.addConstr(lhs == z[i])
#     lhs.clear()
#
# for k in range(vehicle.vehicleNum):
#     lhs = LinExpr(0)
#     for j in range(data.OrderNumber):
#         if(j!=0):
#             lhs.addTerms(1,x[0,j,k])
#     model.addConstr(lhs == rsk[k])
#
# for k in range(vehicle.vehicleNum):
#     for h in range(1,data.OrderNumber-1):
#         expr1 = LinExpr(0)
#         expr2 = LinExpr(0)
#         for i in range(data.OrderNumber-1):
#             if(i!=h):
#                 expr1.addTerms(1,x[i,h,k])
#         for j in range(1,data.OrderNumber):
#             if(h!=j):
#                 expr2.addTerms(1,x[h,j,k])
#         model.addConstr(expr1==expr2)
#         expr1.clear()
#         expr2.clear()
#
# for k in range(vehicle.vehicleNum):
#     lhs = LinExpr(0)
#     for j in range(data.OrderNumber-1):
#         if j!=0:
#             lhs.addTerms(1,x[j,data.OrderNumber-1,k])
#     model.addConstr(lhs == rsk[k])
#
# for k in range(vehicle.vehicleNum):
#     model.addConstr(x[0,data.OrderNumber-1,k] == 0)
#
# lhs = LinExpr(0)
#
# for k in range(vehicle.vehicleNum):
#     for i in range(1,data.OrderNumber-1):
#         for j in range(1,data.OrderNumber):
#             if (i!=j):
#                 model.addConstr(tti[i] + customer.distanceMatrix[data.customerNo[i]][data.customerNo[j]]
#                                 + data.serviceTime[i]-tti[j] -BigM + BigM * x[i,j,k]<= 0)
# for k in range(vehicle.vehicleNum):
#     for j in range(1,data.OrderNumber):
#         model.addConstr(customer.distanceMatrix[data.customerNo[0]][data.customerNo[j]]-tti[j]-BigM+BigM*x[0,j,k]<=0)
#
# for i in range(1,data.OrderNumber-1):
#     for k in range(vehicle.vehicleNum):
#         model.addConstr(ttwi[i] - tti[i] + data.dueTime[i] >= 0)
#
# for k in range(vehicle.vehicleNum):
#     lhs = LinExpr(0)
#     for i in range (1,data.OrderNumber-1):
#         for j in range(data.OrderNumber):
#             if (i!=j):
#                 lhs.addTerms(data.orderDemand[i],x[i,j,k])
#     model.addConstr(lhs<=vehicle.vehicleCapacity)
#
# # 导出模型
# model.write('Test2_2.lp')
# # 求解
# model.optimize()
# # 打印结果
# print("\n\n-----optimal value-----")
# print(model.ObjVal)
#
# # for key in y.keys():
# #     if (y[key].x>0):
# #         print(y[key].VarName + ' = ', y[key].x)
# # for key in z.keys():
# #     if (z[key].x>0):
# #         print(z[key].VarName + ' = ', z[key].x)
# for key in x.keys():
#     if (x[key].x>0):
#         print(x[key].VarName + '=', x[key].x)
